/**
* @description : 仓储信息登记
* @descriptionDetail : 仓储信息登记
* @copyright : 浙江烟草
* @author : mc
* @create : 2022-12-05 16:21:53
*/
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<view class="right-icon" @click="confirm">
				确定
			</view>
		</theme-navbar>
		<!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		<fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->
		<view class="content u-page">
			<u-form :error-type="errorType" class="fpad" :model="storage" :label-width="240" label-position="left" ref="uForm">
				<u-form-item :required="true" label="仓储名称" prop="storageName">
					<u-input type="text" :border="false" placeholder="请输入仓储名称" v-model="storage.storageName" />
				</u-form-item>
				<u-form-item :required="true" label="仓储地址" prop="storageAddress">
					<u-input type="text" :border="false" placeholder="请输入仓储地址" v-model="storage.storageAddress" />
				</u-form-item>
				<u-form-item label="仓储照片" label-position="top">
					<view class="zk-img-content">
						<m-upload :deletable="true" :maxCount="picIds" width="150rpx" height="150rpx" :file-list="fileList" :multiple="true" index="fileList" :uploadParam="{'fiGroupCode':$FILE_GROUP_TYPE.m_unlicense_attr}" upApiUrl="uploadBase64ForApp" ref="filePicUpload" @on-list-change="changeUpload">
						</m-upload>
					</view>
				</u-form-item>
			</u-form>
		</view>
	</view>
</template>

<script>
	import mUpload from '@/components/m-upload.vue';
	import {
		PATHROOT,
	} from "@/common/path.config";
	import commService from "@/service/common/common.service";
	import fabButton from '@/components/uni-fab.vue';

	export default {
		components: {
			mUpload,
			fabButton
		},
		onLoad(opt) {
			let p = this.getRouteParam();
			for (let prop in p) {
				if (Object.prototype.hasOwnProperty.call(p, prop)) {
					this.storage[prop] = p[prop];
					// this.fileList = this.storage.fileList;
				}
			}
			if (p.storageUuid) {
				commService.goFiles(p.storageUuid, this.$FILE_GROUP_TYPE.m_unlicense_attr).then((rs) => {
					if (rs.success) {
						rs.data.forEach((item) => {
							this.fileList.push({
								fileId: item.gfiId,
								gfiId: item.gfiId,
							});
						})
					}
				})
			}
		},
		data() {
			return {
				fileRoot: PATHROOT.BASE + "/commonfile/filestore/downloadFile.do",
				errorType: ['border-bottom', 'message', ],
				storage: {},
				rules: {
					storageName: [{
						required: true,
						message: '请输入仓储名称',
						trigger: ['blur', 'change', ],
					}, ],
					storageAddress: [{
						required: true,
						message: '请输入仓储地址',
						trigger: ['blur', 'change', ],
					}, ],
				},
				picIds: 20,
				fileList: [],
				fContent: [{
					iconPath: require('@/static/confirm.png'),
					selectedIconPath: require('@/static/confirm.png'),
					text: '确定',
					active: false,
				}],
			}
		},
		mounted() {
			this.setRightBtns();
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			setRightBtns() {
				// 设置右侧标题
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("确定", () => {
					this.confirm();
					this.setRightBtns();
				}, true, true);
				// #endif
			},
			fabtrigger(btn) {
				if (btn.index == 0) {
					this.confirm();
				}
			},
			// 上传/删除图片事件
			changeUpload(list, key) {
				if (list.length > 0) {
					this.fileList = [...list, ];
				} else {
					this.fileList = [];
				}
			},
			confirm() {
				let self = this;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let pp = {};
						for (let prop in self.storage) {
							if (Object.prototype.hasOwnProperty.call(self.storage, prop)) {
								pp[prop] = self.storage[prop];
							}
						}
						let newFileList = [];
						this.fileList.forEach(itm => {
							let objImg = {
								fileId: itm.gfiId,
								gfiId: itm.gfiId,
								name: itm.gfiStoreName,
								size: itm.gfiSize,
								type: 'image/' + itm.gfiSuffix,
								status: "done",
								url: this.fileRoot + '?tk=' + this.curUser.tk + '&fileId=' + itm.gfiId,
							}
							newFileList.push(objImg);
						});
						pp['fileList'] = newFileList;
						self.backPrev({
							paramKey: "storage",
							param: pp,
						});
					}
				});
			},
		},
	}
</script>

<style scoped lang="scss">
	.right-icon {
		padding: 0 25rpx;
		font-size: 30rpx;
		color: white;
	}

	.fpad {
		padding: 20rpx 30rpx;
	}
</style>
